<?php
/*
 * @Descripttion: 
 * @Author: Twoknives
 * @Date: 2021-07-16 21:26:57
 * @LastEditTime: 2021-12-04 23:52:14
 * @FilePath: \njz\app\admin\controller\manageuser\User.php
 */

namespace app\admin\controller\manageuser;

use app\admin\controller\anx\Adv;
use app\api\model\Agent;
use app\api\model\User as ModelUser;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="用户管理")
 */
class user extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\User();
        
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
                foreach ($list as $k => $v){
                     
  
                    $list[$k] = $v;
                }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    public function qwe(){
        $coupon = Db::name('coupon')->where('release_or_not',1)->order('id desc')->field('id,title as name,title as text')->select()->toArray();
        return json($coupon);
    }
 
    /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['xuanzhong'])){
                $this->error('优惠券不能为空');
            }
            $xuanzhong = explode(',',$post['xuanzhong']);
            $xuanzhong = array_filter($xuanzhong);
          
            foreach($xuanzhong as $k=>$v){
                $data = [
                    'coupon_id'=>$v,
                    'uid'=>$post['xid'],
                    'add_time'=>date('Y-m-d H:i:s',time())
                ];
                Db::name('coupon_user')->insert($data);
                //优惠券订阅消息通知
                // $arr = Db::name('coupon')->where('id',$v)->field('title,money,endtime')->find();
                // $arr['tishi'] = "可前往个人中心-优惠券界面查看"; 
                //$user = Db::name('user')->where('id',$post['xid'])->field('wx_openid')->find();
                //$openid = $user['wx_openid'];
                //$adv->real_authentication($openid,$arr,2);
            }
            $result = [
                'code'=>1,
                'msg' =>'发放成功'
            ];
            return $result;
        }else{
            $post = $this->request->param('id');
            $this->assign('xid',$post);
        }
        return $this->fetch();
    }

    /**
    * @NodeAnotation(title="成为分销商")
    */
    public function modify_level()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['member_level'])){
                $this->error('等级不能为空');
            }
            Db::name("user")->where("id",$post["id"])->update(["member_level"=>$post["member_level"]]);
            $this->success('操作成功', '');
        }
        return $this->fetch();
    }

 
 

    
}